package com.yld.car.common;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;

import android.os.AsyncTask;

/**
 * 上传图片文件的处理器
 * 
 * @author apple
 * 
 */
public class UploadImageTask extends AsyncTask<String, String, String> {

	public UploadImageTask(String destName, String imgName) {
		this.destName = destName;
		this.imgName = imgName;
	}

	private String destName;
	private String imgName;

	@Override
	protected String doInBackground(String... params) {

		try {

			FileInputStream fis = new FileInputStream(new File(destName));

			ByteArrayOutputStream bos = new ByteArrayOutputStream();

			byte[] cache = new byte[fis.available()];
			fis.read(cache);
			bos.write(cache);
			fis.close();
			NetworkProgressUtils utils = NetworkProgressUtils.getInstance();
			HashMap<String, Object> allParams = new HashMap<String, Object>();
			allParams.put("base64String", bos.toByteArray());

			allParams.put("fileName", imgName);
			// mApp.getAllImgNames().add(imgName);
			// publishProgress("");
			Object o = utils.uploadImage(
					ConstantUtils.NAME_SPACE,
					ConstantUtils.ALL_METHROD_NAME.get(23),
					ConstantUtils.NAME_SPACE
							+ ConstantUtils.ALL_METHROD_NAME.get(23),
					ConstantUtils.GET_UP_LOAD_FILE_URL, allParams);
			// System.out.println(o);

			return o.toString();
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;
	}

	@Override
	protected void onProgressUpdate(String... values) {

		// showLoading("正在上传图片，请等待！");
		super.onProgressUpdate(values);
	}

	@Override
	protected void onPostExecute(String result) {
		// hideLoading();

		System.out.println("upload...."+result);
		if (result != null) {
			//System.out.println("===" + result);
		}
		super.onPostExecute(result);
	}

}
